package org.example.Utils;

import org.example.advice.MyAdvice;
import org.example.dao.ObjDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler {

    //增强的接口
    private ObjDao objDao;

    //获得代理对象
    public Object createProxy(ObjDao objDao){
        this.objDao=objDao;
        ClassLoader classLoader = MyProxy.class.getClassLoader();
        Class<?>[] interfaces = objDao.getClass().getInterfaces();

        return Proxy.newProxyInstance(classLoader,interfaces,this);
    }


    //
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        MyAdvice myAdvice = new MyAdvice();

        myAdvice.check();


        Object invoke = method.invoke(objDao, args);

        myAdvice.log();
        return invoke;

    }
}
